import {message,Tabs} from 'antd'
import React, { useState, useRef} from 'react'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect, Link } from 'umi'
import { queryScoreList,addTrainInfo } from '../service'
import CreateForm from './components/CreateForm'
import moment from 'moment';
import {dealStatusList} from "@/utils/constant";

const  { TabPane } = Tabs;
const TableList = (props) => {
  const actionRef = useRef()
  const actionRef2 = useRef()
  const [done, setDone] = useState(false)
  const [visible, setVisible] = useState(false)
  const [current, setCurrent] = useState(undefined)
  const [tabIndex, setTabIndex] = useState("1")
  const columns = [
    {
      title: '编号',
      dataIndex: 'oid',
      hideInSearch: true,
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '姓名',
      dataIndex: 'name',
    },
    {
      title: '公司',
      dataIndex: 'orgname',

    },
    {
      title: '部门',
      dataIndex: 'unitname',
    },
    {
      title: '岗位',
      dataIndex: 'jobname',
    },
    {
      title: '职级',
      dataIndex: 'jobLevel',
      hideInSearch: true,
    },
    {
      title: '转正日期',
      dataIndex: 'regularDay',
      render: (text) => {
        return text ? moment(text).format('YYYY-MM-DD') : ''
      },
      hideInSearch: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          <a onClick={() => employ(record)}>评分</a>
        </>
      ),
    },
  ]

  const alreadyColumns = [
    {
      title: '编号',
      dataIndex: 'oid',
      hideInSearch: true,
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '姓名',
      dataIndex: 'name',
    },
    {
      title: '公司',
      dataIndex: 'orgname',

    },
    {
      title: '部门',
      dataIndex: 'unitname',
    },
    {
      title: '岗位',
      dataIndex: 'jobname',
    },
    {
      title: '职级',
      dataIndex: 'jobLevel',
      hideInSearch: true,
    },
    {
      title: '转正日期',
      dataIndex: 'regularDay',
      render: (text) => {
        return text ? moment(text).format('YYYY-MM-DD') : ''
      },
      hideInSearch: true,
    },
    {
      title: '状态',
      dataIndex: 'regularIsDone',
      hideInSearch: true,
      valueEnum: {
        0: { text: dealStatusList[0].label },
        1: { text: dealStatusList[1].label },
        2: { text: dealStatusList[2].label },
        3: { text: dealStatusList[3].label },
        4: { text: dealStatusList[4].label },
        5: { text: dealStatusList[5].label },
      },
    },
  ]

  const handleCancel = () => {
    setCurrent(null);
    setVisible(false)
  }

  const employ = (item) => {
    setCurrent(item)
    setVisible(true)
  }

  const handleSubmit = (values) => {
    addTrainInfo(values).then((res)=>{
      if(res.flag){
        setDone(false)
        setCurrent(null)
        setVisible(false)
        message.success('评分保存成功！');
        actionRef.current.reload()
      }else{
        message.error(res.error)
      }
    })
  }
  const handleChangTabIndex = (key) => {
    setTabIndex(key)
    if(key === '1'){
      actionRef.current &&  actionRef.current.reload();
    }else{
      actionRef2.current && actionRef2.current.reload();
    }
  }

  return (
    <PageHeaderWrapper>

      <Tabs animated={false} defaultActiveKey="1" onChange={handleChangTabIndex}>
        <TabPane tab="待办理" key="1">
          <ProTable
            actionRef={actionRef}
            rowKey="oid"
            request={(params) => queryScoreList(params,tabIndex)}
            columns={columns}
          />
          <CreateForm
            values={{}}
            done={done}
            current={current}
            visible={visible}
            onCancel={handleCancel}
            onSubmit={handleSubmit}
          />
        </TabPane>
        <TabPane tab="已办理" key="2">
          <ProTable
            actionRef={actionRef2}
            rowKey="oid"
            request={(params) => queryScoreList(params,tabIndex)}
            columns={alreadyColumns}
          />
        </TabPane>
      </Tabs>
    </PageHeaderWrapper>
  )
}

export default connect(({  }) => ({
}))(TableList)
